package client.shoppingCartFrame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import client.shoppingCartFrame.Transaction.Transaction.Item;
import client.shoppingCartFrame.shoppingCart.ShoppingCart;
import client.shoppingCartFrame.shoppingCart.ShoppingCartItem;

public class ShoppingCartTable implements ActionListener, TableModelListener{
	
	String one, three;
	int two;
	JFrame frame;
	JTable table;
	Vector rows;
	Vector columns;
	Vector labels;
	DefaultTableModel tabModel;
	JScrollPane scrollPane;
	JLabel lblMessage;
	JButton cmdAdd,cmdDelete;
	JPanel mainPanel,buttonPanel;
/*	public static void main(String[] args) 
	{
		ShoppingCartTable t=new ShoppingCartTable();
	}*/

	public ShoppingCartTable()
	{
		rows = new Vector();
		columns= new Vector();
		labels = new Vector<Object>();
		String[] columnNames = { "Type", "Quantity","Price"};
		String[] columnName = {"Type"};
		addColumns(columnNames);

		tabModel=new DefaultTableModel();
		tabModel.setDataVector(rows,columns);

		table = new JTable(tabModel){
			public boolean isCellEditable(int rowIndex, int collIndex)
		{
				return false;
		}
		};
		scrollPane= new JScrollPane(table);//ScrollPane

		table.setRowSelectionAllowed(true);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);


		table.getModel().addTableModelListener(this);

		lblMessage=new JLabel("");


		buttonPanel=new JPanel();
		cmdAdd=new JButton("Add Row");
		cmdDelete=new JButton("Delete") ;

		buttonPanel.add(cmdAdd);
		buttonPanel.add(cmdDelete);

		cmdAdd.addActionListener(this);
		cmdDelete.addActionListener(this);

		mainPanel=new JPanel();
		frame=new JFrame("Simple Table Example");
		frame.setSize(400,400);
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add("Center",scrollPane);
		mainPanel.add("South",buttonPanel);
		mainPanel.setBackground(Color.white);
		buttonPanel.setBackground(Color.white);
		table.getParent().setBackground(Color.black);
		frame.getContentPane().add(mainPanel);
		frame.setVisible(true);

	}


	public synchronized void addObject(Object ob) //Add Row
	{
		
			rows.add((Vector)ob);
			
			labels.addElement(ob);
			
			table.addNotify();
	

	}
	
	public synchronized void addObject(Object ob, int position)
	{
		rows.add(position, (Vector) ob);
		
		if(position >= labels.size()){
			labels.addElement(ob);	
		}else{
			labels.insertElementAt((Vector)ob, position);
		}
	}
	
	public synchronized void addObject(Vector v, Object ob)
	{
		rows.add(v);
		labels.addElement(ob);
		table.addNotify();
	}
	
	public synchronized void addObject(Vector v, Object ob, int position)
	{
		rows.add(position, v);
		if (position >= labels.size())
		{
			labels.addElement(ob);
		}else{
			labels.insertElementAt((Vector)ob, position);
		}
		
		table.addNotify();
		}
	
	public synchronized void delObject(Object ob)
	{
		int index = labels.indexOf(ob);
		
		if (index < 0) return;
		
		labels.removeElementAt(index);
		
		rows.remove(index);
		
		table.addNotify();
		
	}
	
	public synchronized Object getSelectedObject()
	{
		int i =table.getSelectedRow();
		if (i == -1) return null;
		
		return labels.elementAt(i);
	}
	
	
	public synchronized Object[] getSelectedObjects()
	{
		int [] selectedItems = table.getSelectedRows();
		
		Object [] whichObjects = new Object [selectedItems.length];
		
		for (int i = 0; i < selectedItems.length; i ++)
		{
			whichObjects[i] = labels.elementAt(i);
		}
		
		return whichObjects;
	}
	
	public int indexOf(Object ob){
		
		return labels.indexOf(ob);
		
	}
	
	public Object objectAt(int index)
	{
		return labels.elementAt(index);
	}
	
	public void replaceObject (Object ob, int index)
	{
		rows.setElementAt((Vector) ob, index);
		
		labels.setElementAt(ob, index);
		
		table.addNotify();
	}
	
	public void replaceObject(Vector v, Object ob, int index)
	{
		rows.setElementAt(v, index);
		
		labels.setElementAt(ob, index);
		
		table.addNotify();
	}
	
	public void deleteAllObjects()
	{
		int [] selectedItems = table.getSelectedColumns();
		
		for(int i = 0; i < selectedItems.length; i++){
			delObject(labels.elementAt(i));
		}
		table.addNotify();
	}

/*	public Vector createElement(String one, int two, BigDecimal three, int four) 
	{
		Vector t = new Vector();
		t.addElement(one);
		t.addElement(two);
		t.addElement(three);
		t.addElement(four);
		return t;
	}*/

	public void addColumns(String[] colName)//Table Columns
	{
		for(int i=0;i<colName.length;i++)
			columns.addElement((String) colName[i]);
	}
	


/*	void deleteRow(int index) 
	{
		if(index!=-1)//At least one Row in Table
		{ 
			rows.removeElementAt(index);
			table.addNotify();
		}

	}//Delete Row
*/
	public void tableChanged(javax.swing.event.TableModelEvent source)     {
		String msg="";
		TableModel tabMod = (TableModel)source.getSource();
		switch (source.getType())
		{
		case TableModelEvent.UPDATE:
			/*msg="Table Value Updated for  cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
			JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);*/
			break;

		}

	}//Table Changed Method

	/*public void selectCell(int row,int col)
	{
		if(row!=-1 && col !=-1)            
		{
			table.setRowSelectionInterval(row,row);
			table.setColumnSelectionInterval(col,col);
		}
	}*/
	

	public void actionPerformed(ActionEvent source)
	{
		if (source.getSource()==(JButton) cmdAdd)
		{
			//addRow("TypeX", "QuantityX", "PriceX");
		}
		if (source.getSource()==(JButton) cmdDelete)
		{
			//deleteRow(table.getSelectedRow());
		}


	}//ActionList

}